#include "hal.h"   /* Include Types Definition */    
#include "ClkGen.h"   /* Include Types Definition */    

static      unsigned int Counter;
static      unsigned long CountWatch; 
static      unsigned int LastCount;
volatile    union  uClkGen ClkGen;

void ClkGen_Clk(void){
    Counter++;
    CountWatch++;
}    


void ClkGen_Init (void)
{
    HAL_AllocSyncTickEvent(ClkGen_Clk,500,0);
}

  

void ClkGen_Reset (void)
{
  Counter = LastCount=0;  
}

void ClkGen_Ack(void)
{
    HAL_Enter_CS();
    ClkGen.poly= (Counter ^ LastCount);
    LastCount=Counter;
    HAL_Leave_CS();
}
 
void StartWatch(void){
     HAL_Enter_CS();
     CountWatch=0;
     HAL_Leave_CS();
}
 
unsigned int ReadWatch(void){
     unsigned long temp;
     HAL_Enter_CS();
     temp=CountWatch;
     HAL_Leave_CS();
     return((unsigned int)((temp<<4)/3125));
}
 
void SetWatch(unsigned int time){
     unsigned long temp;
     temp= ((((unsigned long)time ) * 3125)>>4);
     HAL_Enter_CS();
     CountWatch=temp;
     HAL_Leave_CS();    
}

unsigned int ClkGen_Read(void){
    return(Counter);
} 
